package shuati.qihu360;

import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;

public class computer
{
    static int a,b;
  public static void main(String args[])
  {
    Scanner sc = new Scanner(System.in);
    int ans=0;
    a=sc.nextInt();
      b=sc.nextInt();
      Map<Integer,Integer> map=new HashMap<>();
      PriorityQueue<Integer> maxheap=new PriorityQueue<>((o1, o2)->o2.compareTo(o1));
      PriorityQueue<Integer> minheap=new PriorityQueue<>();
      for(int i=1;i<=a;i++){
          int [] op=new int[3];
          op=generateQuery();
          //插入
          if(op[0]==1){
              map.put(op[2],map.getOrDefault(op[2],0)+1);
                maxheap.offer(op[2]);
              minheap.offer(op[2]);
              //删除
          }else if(op[0]==2){
              int count=map.getOrDefault(op[2],0);
              if(count==0||count<=op[1]){
                  map.put(op[2],0);
              }else{
                   map.put(op[2],count-op[1]);
              }
             //极差 
          }else if(op[0]==3){
              int ma=maxheap.peek();
              if(map.get(ma)==0){
                  maxheap.poll();
              }
              int mi=minheap.peek();
               if(map.get(mi)==0){
                  minheap.poll();
              }
              ans=ans^(i*(ma-mi));
}
      }
      System.out.println(ans);
  }
    public static  int[] generateQuery(){
        b=b^(b<<15);
        b=b^(b>>5);
        b=b^(b<<1);
        b=b%(1<<15);
        int op=(b^(b<<7)^(b>>5))%3+1;
        int m=(b^(b<<6)^(b>>10))%a+1;
        int x=(b^(b<<5)^(b<<9)^(b>>6))%10+1;
        return new int[]{op,m,x};
    }
}